package com.nhjf.admin.controller.purchaseHistory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nhjf.admin.util.Jurisdiction;
import com.nhjf.common.page.Pagination;
import com.nhjf.model.payLog.PayLog;
import com.nhjf.model.system.Role;
import com.nhjf.model.system.User;
import com.nhjf.pojo.qo.payLog.PayLogQo;
import com.nhjf.service.payLog.PayLogService;

@RequestMapping("/purchaseHistory")
@Controller
public class PurchaseHistoryController {
	@Autowired
	private PayLogService logService;
	/**
	 * 订单列表
	 * @param payLogQo
	 * @param model
	 * @param page
	 * @return
	 */
	@RequestMapping("/viewRecord")
	public String viewRecord(PayLogQo payLogQo,Model model,@RequestParam(value="page", defaultValue="1")Integer page) {
		if(payLogQo==null) {
			payLogQo=new PayLogQo();
		}
		User user=(User)Jurisdiction.getSession().getAttribute(com.nhjf.common.Constants.USER_INFO);
		Set<Role> set= user.getRoleList();
		Iterator<Role> iter = set.iterator();
		Role roles=new Role();
		while(iter.hasNext()) {
			roles=iter.next();
			break;
		}
		if("1".equals(roles.getNumber())) {
			payLogQo.setSeachCoursesSeries(true);
			payLogQo.setSeachUser(true);
			Pagination pagination = new Pagination();
			pagination.setPageSize(10);
			pagination.setCondition(payLogQo);
			pagination.setPageNo(page);
			model.addAttribute("payLogQo", payLogQo);
			pagination = logService.queryPagination(pagination);
			List<PayLog> pList=(List<PayLog>) pagination.getList();
			for (PayLog payLog : pList) {
				if(payLog.getMoney()!=null) {
					payLog.setMoney(payLog.getMoney()/100);
				}
				
			}
			pagination.setList(pList);
			model.addAttribute("pagination", pagination);
			return "/purchaseHistory/purchaseHistory.html";
		}else {
			return "500";
		}

	}
	@RequestMapping("/view")
	public String view(String id,Model model) {
		User user=(User)Jurisdiction.getSession().getAttribute(com.nhjf.common.Constants.USER_INFO);
		Set<Role> set= user.getRoleList();
		Iterator<Role> iter = set.iterator();
		Role roles=new Role();
		while(iter.hasNext()) {
			roles=iter.next();
			break;
		}
		if("1".equals(roles.getNumber())) {
			PayLogQo logQo=new PayLogQo();
			logQo.setSeachCoursesSeries(true);
			logQo.setSeachUser(true);
			logQo.setPayLogId(Long.parseLong(id));
			PayLog log= logService.queryUnique(logQo);
			model.addAttribute("log",log);
			return "/purchaseHistory/purchaseHistory_view.html";
		}else {
			return "500";
		}
	}
}
